<template>
  <div>
    <el-form label-position="left" label-width="120px" style="width:100%;">
      <el-form-item label="车牌号："><el-input v-model="formData.plateNumber" placeholder="请输入车牌号" class="filter-item" /></el-form-item>
      <el-form-item label="所属公司："><el-input v-model="formData.company" placeholder="请输入所属公司明" class="filter-item" /></el-form-item>
      <el-form-item label="黑名单原因："><el-input v-model="formData.reason" placeholder="请输入黑名单原因" class="filter-item" /></el-form-item>
      <el-form-item label="过期时间："><el-date-picker v-model="formData.expireTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss" placeholder="请选择过期时间" :editable="false" class="filter-item" /></el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button icon="el-icon-close" @click="cancel">取消</el-button>
      <el-button v-waves :loading="confirmLoading" type="primary" icon="el-icon-check" @click="submit">确定</el-button>
    </div>
  </div>
</template>
<script>
import { getBlacklistDetail, addBlacklist, editBlacklist } from '@/api/blacklist';
import waves from '@/directive/waves';
import { parseTime } from '@/utils';

export default {
  name: 'BlacklistAdd',
  directives: { waves },
  props: {
    id: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      confirmLoading: false,
      formData: {
      }
    };
  },
  created() {
    const nowDate = new Date();
    let expireDate = nowDate.setDate(nowDate.getDate() + 6);
    this.formData.expireTime = parseTime(expireDate, '{y}-{m}-{d}') + ' 23:59:59';
    if (this.id) {
      this.getDetail(this.id);
    }
  },
  methods: {
    async getDetail(id) {
      await getBlacklistDetail(id).then(response => {
        this.formData = response.data;
      });
    },
    async submit() {
      if (this.id) {
        if (this.formData.plateNumber === '') {
          this.$message.error('请输入车牌号');
          return;
        }
        if (this.formData.expireTime === '' || this.formData.expireTime === null) {
          this.$message.error('请选择过期时间');
          return;
        }
      }
      this.confirmLoading = true;
      if (this.id) {
        await editBlacklist(this.formData).then(res => {
          this.confirmLoading = false;
          if (res.msg !== 'OK') {
            this.$message.error(res.msg);
            return;
          }
          this.$message.success('编辑成功');
          this.$emit('cancelClose', true, 'edit');
        });
      } else {
        await addBlacklist(this.formData).then(res => {
          this.confirmLoading = false;
          if (res.msg !== 'OK') {
            this.$message.error(res.msg);
            return;
          }
          this.$message.success('新增成功');
          this.$emit('cancelClose', true, 'add');
        });
      }
    },
    cancel() {
      if (this.id) {
        this.$emit('cancelClose', false, 'edit');
      } else {
        this.$emit('cancelClose', false, 'add');
      }
    }
  }
};
</script>
